<?php

namespace addons\shopro\controller;

use app\common\model\VideoModel;

class Video extends Base
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 获取视频列表
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $videoModel = new VideoModel();
        $page       = input('param.page/d', 1);
        $result     = $videoModel->getLists([
            'status' => 1,
        ], 10, $page);

        $this->success('获取视频列表成功', $result);
    }

    /**
     * 视频详情
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detail()
    {
        $videoModel = new VideoModel();
        $id         = input('param.id/d');

        if (empty($id)) {
            return json([
                'code' => 401,
                'msg'  => '参数错误'
            ]);
        }

        $detail = $videoModel->getItem($id);

        if (empty($detail)) {
            return json([
                'code' => 402,
                'msg'  => '您要查看的视频不存在'
            ]);
        }
        if ($detail->status == 2) {
            return json([
                'code' => 402,
                'msg'  => '您要查看的视频已下架'
            ]);
        }

        // 浏览量+1
        $videoModel->where(['id' => $id])->setInc('views');

        $detail->create_time = date('Y-m-d H:i', $detail->create_time);

        $this->success('获取成功', $detail);
    }

    /**
     * 播放量统计
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function playNum()
    {
        $videoModel = new VideoModel();
        $id         = input('param.id/d');

        if (empty($id)) {
            return json([
                'code' => 401,
                'msg'  => '参数错误'
            ]);
        }

        $detail = $videoModel->getItem($id);

        if (empty($detail)) {
            return json([
                'code' => 402,
                'msg'  => '您要查看的视频不存在'
            ]);
        }
        if ($detail->status == 2) {
            return json([
                'code' => 402,
                'msg'  => '您要查看的视频已下架'
            ]);
        }

        // 播放量+1
        $videoModel->where(['id' => $id])->setInc('play_num');

        $this->success('获取成功', $detail);
    }

}
